package com.rjwh.dingdong.client.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.localbean.HomeWorkListItem;
import com.rjwh.dingdong.client.bean.localbean.HomeWorkListMenuItem;
import com.rjwh.dingdong.client.util.LogUtil;
import com.rjwh.dingdong.client.util.UrlUtil;
import com.rjwh.dingdong.client.util.ViewHolder;

import java.util.List;

/**
 * @author 王小珂
 * @ClassName HomeWorkShareListAdapter
 * @Description
 * @date 2017-1-4
 * @history 2017-1-4 author: description:
 */
public class HomeWorkShareListAdapter extends AdapterBase<HomeWorkListItem> {

    private Context context;
    private int currentPosition = -1;
    private OnShareBtnListener listener;

    public HomeWorkShareListAdapter(Context context, OnShareBtnListener listener) {
        this.context = context;
        this.listener = listener;
    }

    @Override
    protected View getExView(int position, View convertView, ViewGroup parent) throws Exception {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.home_work_share_list_item, null);
        }

        TextView homeWorkShareListItemImg = ViewHolder.get(convertView, R.id.home_work_share_list_item_img);
        TextView homeWorkShareListItemTitleTv = ViewHolder.get(convertView, R.id.home_work_share_list_item_title_tv);
        TextView homeWorkShareListItemSubtitleTv = ViewHolder.get(convertView, R.id.home_work_share_list_item_subtitle_tv);
        TextView homeWorkShareListItemShareBtn = ViewHolder.get(convertView, R.id.home_work_share_list_item_share_btn);
        TextView homeWorkShareListItemDescTv = ViewHolder.get(convertView, R.id.home_work_share_list_item_desc_tv);
        LinearLayout homeWorkShareListItemMenuRootLl = ViewHolder.get(convertView, R.id.home_work_share_list_item_menu_root_ll);

        HomeWorkListItem homeWorkListItem = getList().get(position);
        if (homeWorkListItem == null) {
            return convertView;
        }
        String lessonNO = homeWorkListItem.getLessonNO();
        String bgcolor = homeWorkListItem.getBgcolor();
        String title = homeWorkListItem.getTitle();
        String subtitle = homeWorkListItem.getSubtitle();
        final String content = homeWorkListItem.getContent();
        final String actionurl = homeWorkListItem.getActionurl();
        String canshare = homeWorkListItem.getCanshare();
        List<HomeWorkListMenuItem> menus = homeWorkListItem.getMenus();
        final String bookid = homeWorkListItem.getBookid();
        final String operation = homeWorkListItem.getOperation();

        if (menus != null && !menus.isEmpty()) {
            //动态生成menu
            homeWorkShareListItemMenuRootLl.removeAllViews();
            for (final HomeWorkListMenuItem menu : menus) {
                final TextView textView = new TextView(context);
                textView.setText(menu.getTitle());
                textView.setTextColor(context.getResources().getColor(R.color.content_text_color));
                textView.setTextSize(14);
                textView.setGravity(Gravity.CENTER);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
                layoutParams.setMargins(0, 20, 0, 20);
                textView.setLayoutParams(layoutParams);
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UrlUtil.handelUrl(context, menu.getActionurl(), true);
                        LogUtil.d("HomeWorkShareListAdapte", "Actionurl: " + menu.getActionurl());
                    }
                });
                homeWorkShareListItemMenuRootLl.addView(textView);
            }
        }

        if (!AsStrUtils.isEmpty(lessonNO)) {
            homeWorkShareListItemImg.setText(lessonNO);
        }
        if (!AsStrUtils.isEmpty(title)) {
            homeWorkShareListItemTitleTv.setText(title);
        }
        if (!AsStrUtils.isEmpty(subtitle)) {
            homeWorkShareListItemSubtitleTv.setText(subtitle);
        }
        if (!AsStrUtils.isEmpty(content)) {
            homeWorkShareListItemDescTv.setText(content);
        }
        if (!AsStrUtils.isEmpty(canshare) && canshare.equals("1")) {
            homeWorkShareListItemShareBtn.setVisibility(View.VISIBLE);
        } else {
            homeWorkShareListItemShareBtn.setVisibility(View.GONE);
        }

        if (!AsStrUtils.isEmpty(bgcolor)) {
            String[] split = bgcolor.split(",");
            String s1 = split[0];
            String s2 = split[1];
            String s3 = split[2];
            int rgb = Color.rgb(Integer.parseInt(s1), Integer.parseInt(s2), Integer.parseInt(s3));
            GradientDrawable background = (GradientDrawable) homeWorkShareListItemImg.getBackground();
            background.setColor(rgb);
        }


        homeWorkShareListItemShareBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 分享作业
                if (listener != null) {
                    listener.onShareBtnClick(bookid, actionurl, operation);
                }
            }
        });

        homeWorkShareListItemDescTv.setVisibility(currentPosition == position && !AsStrUtils.isEmpty(content) ? View.VISIBLE : View.GONE);
        homeWorkShareListItemMenuRootLl.setVisibility(currentPosition == position ? View.VISIBLE : View.GONE);

        return convertView;
    }

    public void setSelectPosition(int currentPosition) {
        this.currentPosition = currentPosition;
    }

    @Override
    public void appendToList(List<HomeWorkListItem> list) {
        super.appendToList(list);
        currentPosition = -1;//刷新时使内容收起
    }

    public interface OnShareBtnListener {
        void onShareBtnClick(String bookid, String actionurl, String operation);
    }
}
